package com.lb.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lb.common.Result;
import com.lb.entity.DTO.shopDTO;
import com.lb.entity.Shop;
import com.lb.entity.VO.OrderVo;
import com.lb.entity.VO.ShopVo;
import com.lb.service.ShopService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RestController
@RequestMapping
@CrossOrigin
public class shopcontroller {
    @Autowired
    private ShopService shopService;
    @PostMapping("/shop/add")
    @Transactional
    public Result add(@RequestBody shopDTO dto) {
        Shop shop = new Shop();
        BeanUtils.copyProperties(dto, shop);
        shopService.saveOrUpdate(shop);
        return Result.success();
    }
    @GetMapping("/shop/get")
    public Result<OrderVo> getAll(@RequestParam String userName) {
      // List<ShopVo> list=shopService.get(userName);
        //return Result.success(list);
        double totalSum = 0.0;
        List<ShopVo> list = shopService.get(userName);
        for (ShopVo shopVo : list) {
            int number = shopVo.getNumber();
            Double price = shopVo.getPrice();
            Double total = number * price;
            totalSum += total;
        }
        OrderVo orderVo = new OrderVo();
        orderVo.setTotal(totalSum);
        orderVo.setShops(list);
        return Result.success(orderVo);
    }
    @GetMapping("/shop/delete")
    @Transactional
    public  Result delete(@RequestParam String userName) {
        QueryWrapper<Shop> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userName", userName);
        shopService.remove(queryWrapper);
        return Result.success();
    }
}
